package com.ly.blog2Word.service.impl;

import com.ly.blog2Common.core.page.MybatisPageHelper;
import com.ly.blog2Common.core.page.PageRequest;
import com.ly.blog2Common.core.page.PageResult;
import com.ly.blog2Common.utils.DateTimeUtils;
import com.ly.blog2Word.dao.WordSearchMapper;
import com.ly.blog2Word.dao.WordSearchMapper;
import com.ly.blog2Word.dao.model.WordSearch;
import com.ly.blog2Word.service.WordSearchService;
import com.ly.blog2Word.service.WordSearchService;
import com.ly.blog2Word.utils.ArticleUtils;
import io.micrometer.core.instrument.search.Search;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author: lyMaster
 * @time: 2020/7/17 19:07
 */
@Service
public class WordSearchImpl implements WordSearchService {

    @Autowired
    private WordSearchMapper wordSearchMapper;

    @Override
    public int save(WordSearch record) {
        if(record.getId() == null || record.getId() == 0) {
            return wordSearchMapper.insertSelective(record);
        }
        return wordSearchMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int delete(WordSearch record) {
        return wordSearchMapper.deleteByPrimaryKey(record.getId());
    }

    @Override
    public int delete(List<WordSearch> records) {
        for(WordSearch record:records) {
            delete(record);
        }
        return 1;
    }

    @Override
    public WordSearch findById(Long id) {
        return wordSearchMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageResult findPage(PageRequest pageRequest) {
        return MybatisPageHelper.findPage(pageRequest, wordSearchMapper);
    }

//    @Override
//    public Object findPage() {
//        return wordSearchMapper.findPage();
//    }

    @Override
    public Object findSearchList(String searchKey) {
        List<Map<String,String>> labelList= wordSearchMapper.findSearchList("%"+searchKey+"%");

        List<WordSearch>  wordSearchList = wordSearchMapper.findPage();
        WordSearch wordSearch = new WordSearch();
        wordSearch.setSearchKey(searchKey);
        wordSearch.setTimes("1");
        for(WordSearch search:wordSearchList){
            if(searchKey.equals(search.getSearchKey())){
                //触发修改
                search.setTimes((Integer.parseInt(search.getTimes()) + 1)+"");
                search.setLastUpdateTime(new Date());
                wordSearch = search;
                break;
            }
        }
        save(wordSearch);


        for(Object o:labelList){
            if(o instanceof Map){
                Map<String,String> contentObj = (Map<String,String>) o;
                String content = contentObj.get("content");
                content = ArticleUtils.subStringWithLengthWithKey(content,searchKey);
                contentObj.put("content",content);
            }
        }

        return labelList;

    }

}
